import MySQLdb
import datetime
import operator
from DatabaseConnector import connection

class CDALVersion:
    
    def __new__(cls, *args, **kargs):
        if cls.instance is None:
            cls.instance = object.__new__(cls, *args, **kargs)
        return cls.instance   
    
    def __init__(self):
        cursor = connection.cursor()
        cursor.execute("select max(version_id) from snomed.cdal_version")
        latest_vs = cursor.fetchone()[0]
        cursor.execute("select version_id, round_id from snomed.log where processed_date_time in (select max(processed_date_time) from snomed.log)")
        cur_vs = "0"
        if cursor.rowcount != 0:
            tuple = cursor.fetchone()
            cur_vs = tuple[0]
            cur_rnd = tuple[1]
        if latest_vs > cur_vs:
            self.round = "1"        
            self.version = latest_vs
        elif latest_vs == cur_vs:
            self.version = latest_vs
            self.round = str(int(cur_rnd) + 1)
        else:
            print "There is a version conflict..."
        
        current_time = self.getCurrentTime()
        
#        print self.version, self.round 
#        print self.current_time
             
    
if __name__ == "__main__":
    cdalvs = CDALVersion()
    cdalvs.getLatestVersion()  
    
        
    